home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 …ember: Reference Library / Dev.CD Dec 00 RL Disk 1.toast / pc / technical documentation / develop / develop issue 28 / develop issue 28 code / sketch / source / utilities / sounds.c next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  5.5 KB  |  256 lines

  1. /****************************************************************************
  2.  * 
  3.  * Sounds.c
  4.  * 
  5.  ****************************************************************************/
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9. #include <Sound.h>
  10. #include <Speech.h>
  11.  
  12. #include "Sounds.h"
  13. #include "StringUtils.h"
  14.  
  15. // ------------------------------------------------------------------------------------------------
  16.  
  17. static SpeechChannel    fgChannel;
  18. static short            fgInstalledVoice            = 0;
  19. static Boolean            fgPrintErrorMessages     = false;
  20. static Boolean            fgSpeechMgrPresent        = false;
  21. static Boolean            fgSpeechInitialized        = false;
  22.  
  23.  
  24. // ------------------------------------------------------------------------------------------------
  25.  
  26. static OSErr            SayCString            (char *string);
  27. static Boolean            GetVoiceByName        (char *name, VoiceSpec *voice);
  28. static Boolean            InstallVoice        (char *name);
  29.  
  30. // ---------------------------------------------------------------------
  31. // This function plays a 'snd ' resource
  32.  
  33. void 
  34. PlaySound(short sndResourceID)
  35. {
  36.     OSErr                error;
  37.     Boolean             kAsync         = true;
  38.     SndChannelPtr     sndChannel     = NULL;
  39.      Handle             sndHandle     = nil;
  40.   
  41.    // Apple Guidelines for resource id's
  42.     //   all negative numbers are reserved for Apple's use
  43.     //   0—127    reserved for Apple use
  44.     
  45.     if (sndResourceID < 1000) {
  46.       sndResourceID = 1000; 
  47.     }
  48.         
  49.     sndHandle = GetResource('snd ', sndResourceID);
  50.     
  51.     if (sndHandle == nil)
  52.     {
  53.         SysBeep(2);
  54.     }
  55.     else
  56.     {
  57.         error = SndPlay(sndChannel, (SndListHandle)sndHandle, kAsync);
  58.         ReleaseResource(sndHandle);
  59.     }
  60. }
  61.  
  62. // ---------------------------------------------------------------------
  63. //                             Speech Manager support
  64. // ---------------------------------------------------------------------
  65.  
  66. OSErr
  67. InitSpeechManager(void) 
  68. {
  69.     long feature = 0L;
  70.     
  71.     OSErr err      = Gestalt(gestaltSpeechAttr, &feature);
  72.     
  73.     if (err == noErr) {
  74.        fgSpeechMgrPresent = feature & 1L;
  75.     }
  76.     
  77.     fgSpeechInitialized = true;
  78.     
  79.     return (noErr);
  80. }
  81.  
  82. // ---------------------------------------------------------------------
  83. // This function speaks a c-style string if the Speech Manager is installed
  84. //
  85. // Example call:
  86. //        Say("Victoria",     "Victoria speaking!");    // Use Victoria voice with MacInTalk Pro synth
  87. //     Say(NULL,             "NULL voice speaking!"); // use default synthesizer/voice
  88. // ---------------------------------------------------------------------
  89.  
  90. void 
  91. Say(char *voiceName, char *message)
  92. {
  93.     if (fgSpeechInitialized == false) {
  94.         InitSpeechManager();
  95.     }
  96.     
  97.     if (fgSpeechMgrPresent == false) {
  98.         SysBeep(2);
  99.         return;
  100.     }
  101.         
  102.     if (InstallVoice(voiceName)) {
  103.         SayCString(message);
  104.     }
  105. }
  106.  
  107. // ---------------------------------------------------------------------
  108. // This function always uses the default voice and synthesizer
  109. // SInce it takes a Pascal string, this can easily be used in 
  110. // Dialog box routines
  111.  
  112. OSErr
  113. SayPString(StringPtr string)
  114. {
  115.     OSErr error            = noErr;
  116.     
  117.     if (fgSpeechInitialized == false) {
  118.         InitSpeechManager();
  119.     }
  120.     
  121.     if (fgSpeechMgrPresent)
  122.     {
  123.         if (InstallVoice(NULL))
  124.         {
  125.             while (SpeechBusy())
  126.                 ;    // wait politely while the other person finishes talking!
  127.             error = SpeakString(string);
  128.         }
  129.         else
  130.         {
  131.             SysBeep(2);
  132.         }
  133.     }
  134.     else
  135.     {
  136.         SysBeep(2);
  137.     }
  138.         
  139.     return error;
  140. }
  141.  
  142. // ---------------------------------------------------------------------
  143. // This function speaks a text handle if the Speech Manager is installed
  144. // ---------------------------------------------------------------------
  145.  
  146. OSErr 
  147. SayHandle(char *voiceName, Handle message)
  148. {
  149.     OSErr error = noErr;
  150.     char savedState;
  151.     
  152.     if (fgSpeechInitialized == false) 
  153.     {
  154.         error = InitSpeechManager();
  155.     }
  156.     
  157.     if (error == noErr && fgSpeechMgrPresent) 
  158.     {
  159.         if (InstallVoice(voiceName)) 
  160.         {
  161.             while (SpeechBusy())
  162.                 ;    // wait politely while the other person finishes talking!
  163.             
  164.             savedState = HGetState(message);
  165.             HLock(message);
  166.             error = SpeakText(fgChannel, (char *)*message, GetHandleSize(message));
  167.             HSetState(message, savedState);
  168.         }
  169.     }
  170.     return error;
  171. }
  172.  
  173. // ---------------------------------------------------------------------
  174.  
  175. static OSErr
  176. SayCString(char *string)
  177. {
  178.     OSErr error = noErr;
  179.  
  180.     while (SpeechBusy())
  181.         ;    // wait politely while the other person finishes talking!
  182.     
  183.     error = SpeakText(fgChannel, string, strlen(string));
  184.     if (error != noErr)
  185.         SysBeep(2);
  186.     
  187.     return error;
  188. }
  189.  
  190. // ---------------------------------------------------------------------
  191.  
  192. static Boolean    
  193. InstallVoice(char *name)
  194. {
  195.     OSErr         error         = noErr;
  196.     VoiceSpec     voice;
  197.     Boolean         foundIt         = false;
  198.     VoiceSpec  *voicePtr     = &voice;
  199.     
  200.     if (name == NULL || name[0] == '\0') {
  201.         voicePtr = NULL;  // use default system voice
  202.     }
  203.     else 
  204.     {
  205.         foundIt = GetVoiceByName(name, &voice);
  206.         if (!foundIt) 
  207.         {
  208.             voicePtr = NULL;
  209.         }
  210.     }
  211.     
  212.     if (fgChannel != nil) 
  213.     {
  214.         DisposeSpeechChannel(fgChannel);
  215.     }
  216.  
  217.     error = NewSpeechChannel(voicePtr, (SpeechChannel*)&fgChannel);
  218.     
  219.     return (error == noErr);
  220. }
  221.  
  222. // ---------------------------------------------------------------------
  223.  
  224. static Boolean
  225. GetVoiceByName(char *name, VoiceSpec *voice)
  226. {
  227.     VoiceSpec              spec;
  228.     VoiceDescription     desc;
  229.     char                     temp[63+1]   = {0};
  230.     short                  i;
  231.     short                  numVoices;
  232.     Boolean                 foundIt = false;
  233.     
  234.     CountVoices(&numVoices);
  235.     for (i=1; i <= numVoices && !foundIt; i++) 
  236.     {
  237.         GetIndVoice(i, &spec);
  238.         GetVoiceDescription(&spec, &desc, sizeof(desc));
  239.         BlockMoveData((Ptr)&desc.name, temp, desc.name[0]+1);
  240.         PToCString((StringPtr)temp, temp);
  241.         if (strncmp(name, temp, strlen(name)) == 0) 
  242.         {
  243.             foundIt = true;
  244.         }
  245.     }
  246.     
  247.     if (foundIt) 
  248.     {
  249.         MakeVoiceSpec(spec.creator, spec.id, voice);
  250.     }
  251.     
  252.     return foundIt;
  253. }
  254.  
  255. // ---------------------------------------------------------------------
  256.